(function($) {
	/* 面板的最大z-index */
	var g_maxZIndex = 0;

	/**
	 * HESCGIS_Panel插件
	 */
	$.extend($.fn, {
		HESCGIS_Panel: function(settings) {
			var init = $.extend({}, $.fn.HESCGIS_Panel.defaults, settings);

			this.each(function() {
				var $this = $(this);

				if ($this.html() === null) {
					throw new Error("无法在当前对象中添加HESCGIS_Panel对象。");
				}
				var $header;
				var $title;
				var $titleText = $('<span>').text(init.header)
				.addClass('HESCGIS_PanelTitleText');
				if (init.enableClose == false) { //添加最小化面板按钮
					var $minimizer = $('<span>').addClass(init.panelCloserCss)
					.addClass('HESCGIS_PanelUp')
					.bind('click', $this, init.minimizerClickHandler);
					$header = $('<div>').addClass(init.panelHeaderCss);
					$title = $('<div>').addClass(init.panelTitleCss)
					.append($titleText, $minimizer);
				} else { //添加关闭面板按钮
					/* 构建面板元素 */
					var $closer = $('<span>').addClass(init.panelCloserCss)
						.addClass('HESCGIS_PanelRemove')
						.bind('click', $this, init.closerClickHandler);
					$header = $('<div>').addClass(init.panelHeaderCss);
					$title = $('<div>').addClass(init.panelTitleCss)
					.append($titleText, $closer);
				}
				var $content = $('<div>').addClass(init.panelContentCss)
					.html(init.htmlContent);
				var $min = $('<div>').addClass('HESCGIS_MinPanel')
					.append($header, $title);
				$this.addClass(init.panelCss).css({
					'top': init.top,
					'right': init.right,
					'bottom': init.bottom,
					'left': init.left,
					'width': init.width,
					'height': init.height,
					'z-index': g_maxZIndex++,
				}).append($min, $content).appendTo(init.target).click(bringToForefront);

				/* 自定义操作参数设置 */
				if (init.draggable) {
					$this.draggable({
						containment: "parent",
						cancel: "." + init.panelContentCss // 面板内容不允许拖动，解决滚动条Bug
					});
				}
				if (init.resizable) {
					$this.resizable({
						resize: resizeHandler
					});
				}

				$(window).resize(resizeHandler); // 解决浏览器尺寸变化造成的变形
				resizeHandler(); // 面板创建后执行一次尺寸调整

				/**
				 * 调整面板元素的高度，解决滚动条问题
				 */
				function resizeHandler() {
					var panelHeight = $this.height();
					var headerHeight = $header.outerHeight(true);
					$content.height(panelHeight - headerHeight);
				}
			});

			$.extend(this, {
				setHtmlContent: setHtmlContent,
				appendHtmlContent: appendHtmlContent,
				prependHtmlContent: prependHtmlContent,
				resetHtmlContent: resetHtmlContent,
				bringToFront: bringToForefront
			});
			return this;
		}
	});

	/**
	 * HESCGIS_Panel默认初始化参数
	 */
	$.fn.HESCGIS_Panel.defaults = {
		target: "body",
		header: "面板",
		htmlContent: "<div class='text-center text-warning'>无内容</div>",
		panelCss: "HESCGIS_Panel",
		panelHeaderCss: "HESCGIS_PanelHeader",
		panelTitleCss: "HESCGIS_PanelTitle",
		panelCloserCss: "HESCGIS_PanelCloser",
		panelContentCss: "HESCGIS_PanelContent",
		draggable: true,
		resizable: true,
		top: 80,
		right: undefined,
		bottom: undefined,
		left: 40,
		width: 350,
		height: 400,
		closerClickHandler: function(evt) {
			var $panel = evt.data;
			$panel.hide();
		},
		minimizerClickHandler: function(evt) {
			var $panel = evt.data;
			var $minimizerIconSpan = $panel.find('.HESCGIS_PanelCloser');
			if($minimizerIconSpan.hasClass('HESCGIS_PanelUp')){
				$minimizerIconSpan.removeClass('HESCGIS_PanelUp').addClass('HESCGIS_PanelDown');
				var $content = $panel.find('.HESCGIS_PanelContent');
				$content.slideUp('normal');
			}else{
				$minimizerIconSpan.removeClass('HESCGIS_PanelDown').addClass('HESCGIS_PanelUp');
				var $content = $panel.find('.HESCGIS_PanelContent');
				$content.slideDown('normal');
			}
		}
	};

	/**
	 * 设置面板的HTML内容
	 * @param {string|jQuery|HTMLElement} HTML内容
	 */
	function setHtmlContent(htmlContent) {
		return this.each(function() {
			$(this).children('div:eq(1)').html(htmlContent);
		});
	}

	/**
	 * 在面板已有的HTML内容之后追加内容
	 * @param {string|jQuery|HTMLElement} HTML内容
	 */
	function appendHtmlContent(htmlContent) {
		return this.each(function() {
			$(this).children('div:eq(1)').append(htmlContent);
		});
	}

	/**
	 * 在面板已有的HTML内容之前追加内容
	 * @param {string|jQuery|HTMLElement} HTML内容
	 */
	function prependHtmlContent(htmlContent) {
		return this.each(function() {
			$(this).children('div:eq(1)').prepend(htmlContent);
		});
	}

	/**
	 * 重置面板的HTML内容
	 * @param {boolean} 是否包含默认的HTML内容
	 */
	function resetHtmlContent(withDefaultContent) {
		return this.each(function() {
			var $content = $(this).children('div:eq(1)');
			if (withDefaultContent)
				$content.html($.fn.HESCGIS_Panel.defaults.htmlContent);
			else
				$content.empty();
		});
	}

	/**
	 * 将面板置于最前
	 */
	function bringToForefront() {
		var $this = $(this);
		// 若当前面板已是最前，则无需修改z-index
		if ($this.length == 1 && parseInt($this.css('z-index')) + 1 == g_maxZIndex)
			return $this;
		else
			return $this.each(function() {
				$(this).css('z-index', g_maxZIndex++);
			});
	}
})(jQuery);
